<template>
  <PageMain :title="id ? '编辑' :'新增'">
    <div class="form-content f-c j-b">
      <div class="form-box">
        <el-form ref="formRef" style="width: 660px" :model="formData" :rules="rules" label-position="top" >
         <el-row :gutter="20">
           <el-col :span="12">
             <el-form-item label="标题" prop="title">
               <el-input clearable v-model="formData.title"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="点赞数" width="100px" prop="like">
               <el-input type="number" clearable v-model="formData.like"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item label="收藏数" prop="collect">
               <el-input type="number" clearable v-model="formData.collect"></el-input>
             </el-form-item>
           </el-col>
           <el-col :span="24">
             <el-form-item label="图片" style="width: 100%">
               <UploadImage v-model:image="formData.image" v-model:image-url="formData.imageUrl"/>


             </el-form-item>
           </el-col>
           <el-col :span="24">
             <el-form-item label="内容详情" style="width: 100%" prop="details">
               <Editor  v-model="formData.details" />
             </el-form-item>
           </el-col>

         </el-row>
        </el-form>
      </div>
      <div class="form-button f-s">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirm">确认</el-button>
      </div>
    </div>
  </PageMain>
</template>
<script setup>
import {computed, ref} from "vue";
import { useRouter, useRoute } from "vue-router";
import {Plus} from "@element-plus/icons-vue";
import {ElLoading, ElMessage} from "element-plus";
import { httpUpload, http } from "@/api";
const apiKey = 'UserDynamic'
const router = useRouter()
const route = useRoute()
const id = route.params.id

const formData = ref({
  "details": "",
  "image": "",
  "title": "",
  collect: '',
  like: ''
})
const rules = ref({
  title: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
  details: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
  collect: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
  like: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
})
const formRef =  ref(null)
if (id) {
  // 获取详情
  http[[apiKey]].read({
    id
  }).then(res => {
    for (const valueKey in formData.value) {
      formData.value[valueKey] = res.data[valueKey]
    }
    formData.value.id = id
  })
}
const cancel = () => {
  router.back()
}
const confirm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      const loadingInstance = ElLoading.service()
      if (id) {
        // 编辑
        http[[apiKey]].update({
          ...formData.value,
          id,
          classify: 4
        }).then(res => {
          console.log(res)
          if (res.code === 0) {
            ElMessage.success('编辑成功')
            router.back()
          }
        }).finally(() => {
          loadingInstance.close()
        })
      } else {
        // 新增
        http[[apiKey]].create({
          ...formData.value,
          classify: 4
        }).then(res => {
          console.log(res)
          if (res.code === 0) {
            ElMessage.success('新增成功')
            router.back()
          }
        }).finally(() => {
          loadingInstance.close()
        })
      }
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
</script>

<style lang="scss">
.form-box {
  display: flex;
  .gap {
    width: 32px;
  }
  .avatar-uploader .avatar {
    width: 120px;
    height: 120px;
    display: block;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }

  .avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
  }

  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    text-align: center;
  }
}
</style>
